﻿using CommonLibrary;
using CommonLibrary.Models;
using CommonSite.Areas.API.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BLToolkit.Data.Linq;
using CommonSite.Common;
using CommonHelper;

namespace CommonSite.Areas.API.Controllers
{
    public class FormController : BaseAPIController
    {
        public ActionResult Query()
        {
            using (CommonDatabase db = new CommonDatabase())
            {
                var query = (from p in db.wf_Form
                             where p.State == 1
                             select new Form
                             {
                                 Id = CryptHelper.Encrypt(p.Id),
                                 FormName = p.Name
                             }).ToList();

                return Success(new
                {
                    Total = query.Count,
                    Rows = query
                });
            }
        }

        public ActionResult GetForms()
        {
            using (CommonDatabase db = new CommonDatabase())
            {
                var query = (from p in db.wf_Form
                             where p.State == 1
                             select new
                             {
                                 value = CryptHelper.Encrypt(p.Id),
                                 text = p.Name
                             }).ToList();

                return Success(query);
            }
        }

        public ActionResult Create(string FormName)
        {
            if (!FormName.IsNullOrWhiteSpace())
            {
                try
                {
                    using (CommonDatabase db = new CommonDatabase(CommonOption.ReadWrite))
                    {
                        db.BeginTransaction();
                        if (db.wf_Form.Where(k => k.Name == FormName && k.State == 1)
                              .Count() > 0)
                        {
                            throw new ArgumentException("表单名称不能重复");
                        }

                        db.wf_Form.Insert(() => new wf_Form
                        {
                            Name = FormName
                        });

                        db.CommitTransaction();
                        return Success();
                    }
                }
                catch (Exception ex)
                {
                    return Failed(ex.Message);
                }
            }

            return ArgumentFailed();
        }

        public ActionResult Update(Form Form)
        {
            if (this.ModelState.IsValid)
            {
                try
                {
                    using (CommonDatabase db = new CommonDatabase(CommonOption.ReadWrite))
                    {
                        db.BeginTransaction();
                        int id = CryptHelper.DecryptToInt32(Form.Id);
                        //db.wf_Form.Where(k => k.Id == id)
                        //  .Set(k => k.Name, Form.FormName)
                        //  .Update();

                        db.SetCommand(
                            "update wf_Form set Name = @name, Version = Version + 1 where Id = @id",
                            db.Parameter("@name", Form.FormName),
                            db.Parameter("@id", id))
                          .ExecuteNonQuery();

                        db.CommitTransaction();
                        return Success();
                    }
                }
                catch (Exception ex)
                {
                    return Failed(ex.Message);
                }
            }

            return ArgumentFailed();
        }

        public ActionResult Delete(Form Form)
        {
            try
            {
                using (CommonDatabase db = new CommonDatabase(CommonOption.ReadWrite))
                {
                    db.BeginTransaction();
                    int id = CryptHelper.DecryptToInt32(Form.Id);
                    db.wf_Form.Where(k => k.Id == id)
                      .Set(k => k.State, 0)
                      .Update();

                    db.CommitTransaction();
                    return Success();
                }
            }
            catch (Exception ex)
            {
                return Failed(ex.Message);
            }
        }
    }
}
